package com.yans.managers
{ 
	import com.yans.interfaces.IFileManager;
	
	import flash.events.Event;
	import flash.events.FileListEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.core.ByteArrayAsset;
	
	public class FileManager implements IFileManager
	{
		private var _fileDictionary:Dictionary;
		
		public function FileManager()
		{
			_fileDictionary = new Dictionary;
		}
		
		public function writeFile(fileName:String, byteArray:ByteArray, overWrite:Boolean = true, isCompress:Boolean = false):void
		{ 
			var f:File = new File(fileName);
			if(!overWrite && f.exists) return ;
			if(overWrite && f.exists) f.deleteFile();
			if(!f.parent.exists) f.parent.createDirectory();
			var fs:FileStream = new FileStream();
			fs.open(f, FileMode.WRITE);
			if(isCompress) byteArray.compress();
			fs.writeBytes(byteArray); 
			fs.close();
			trace("#写入文件", fileName);
			fs = null;
			f = null; 
		}
		
		public function createXML(asset:Class):XML
		{
			var ba:ByteArrayAsset = ByteArrayAsset( new asset() ) ;
			var source:String = ba.readUTFBytes( ba.length );
			
			try {
				var xml:XML = new XML( source );
			}
			catch(error:Error)
			{
				throw new Error( "Class must embed an XML document containing valid mark-up. " + error.message );
			}
			return xml;
		}
		
		
		public function writeTextFile(fileName:String, text:String, overWrite:Boolean=true):void
		{ 
			var f:File = new File(fileName);
			if(!overWrite && f.exists) return ;
			var fs:FileStream = new FileStream();
			fs.open(f, FileMode.WRITE);
			fs.writeUTFBytes(text); 
//			fs.writeMultiByte(text, "cn-gb");
			fs.close();
			trace("#写入文件", fileName);
			fs = null;
			f = null; 
		}
		
		public function readFile(fileName:String, callBack:Function, isCompress:Boolean=false):void
		{
			var f:File = new File(fileName);
			if(f.exists) {
				var fs:FileStream = new FileStream();
				fs.open(f, FileMode.READ);
				var bt:ByteArray = new ByteArray;
				fs.readBytes(bt, 0, fs.bytesAvailable);
				if(bt) {
					if(isCompress) {
						bt.uncompress(); 
					} 
					bt.position = 0;
					trace("#读取文件", fileName);
					//					func(bt.readUTFBytes(bt.length));	 
				}
				fs.close();
				callBack(bt);
			}
			else {
				callBack(new ByteArray());
			}
		}
		
		public function readTextFile(fileName:String, callBack:Function, isCompress:Boolean=false):void
		{
			readFile(fileName, function(d:ByteArray):void {
				callBack(d.readUTFBytes(d.length));
			}, isCompress);
		}
		
		public function byteArray2Text(bytArr:ByteArray):String
		{
			return bytArr.readUTFBytes(bytArr.bytesAvailable);
		}
		
		public function text2ByteArray(str:String):ByteArray
		{
			var bytArr:ByteArray = new ByteArray;
			bytArr.writeUTFBytes(str);
			bytArr.position = 0;
			return bytArr;
		}
		
		private var _dicFile:File;
		public function browseDicrctoryForSave(call:Function):void {
			if(!_fileOpen) _fileOpen = new File;
			_fileDictionary[_fileOpen] = call;
			_fileOpen.addEventListener(Event.SELECT, onSaveCompleteSelection);
			_fileOpen.browseForDirectory("请选择一个目录");
		}
		
		private var _fileOpen:File;
		public function browseFileForOpen(call:Function):void {
			if(!_fileOpen) _fileOpen = new File;
			_fileDictionary[_fileOpen] = call;
			_fileOpen.addEventListener(Event.SELECT, onSaveCompleteSelection);
			_fileOpen.browseForOpen("请选择文件"); 
		}
		
		private function onSaveCompleteSelection(e:Event):void {
			var file:File = e.target as File;
			file.removeEventListener(Event.SELECT, onSaveCompleteSelection);
			var callback:Function = _fileDictionary[file] as Function;
			if(null != callback) { 
				delete _fileDictionary[file];
				callback(file.nativePath);
			}
			callback = null;
			file = null;
		}
		
		private var _fileSave:File;
		public function browseFileForSave(call:Function):void {
//			var file:File = new File;
			if(!_fileOpen) _fileOpen = new File;
			_fileDictionary[_fileOpen] = call;
			_fileOpen.addEventListener(Event.SELECT, onSaveCompleteSelection);
			_fileOpen.browseForSave("请选择一个文件");
		}
		
		private var _fileMutile:File;
		public function browseMutileFileForOpen(call:Function):void { 
			if(!_fileOpen) _fileOpen = new File;
			_fileDictionary[_fileOpen] = call;
			_fileOpen.addEventListener(FileListEvent.SELECT_MULTIPLE, onSelectMutileFile);
			_fileOpen.browseForOpenMultiple("请选择多个文件");
		}
		
		private function onSelectMutileFile(e:FileListEvent):void {
			var file:File = e.target as File;
			file.removeEventListener(FileListEvent.SELECT_MULTIPLE, onSelectMutileFile);
			var callback:Function = _fileDictionary[file] as Function;
			if(null != callback) {
				callback(e.files);
				delete _fileDictionary[file];
			}
		}
		
		
	}
}